﻿using System;
using System.Data;
using System.IO;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using webapp4U;
using webapp4U.BOL;

namespace webapp4U.UI
{
    public partial class ItemDetail : BasePage
    {
        private int ProductID = -1;
        public string ImageName = "";
        private int CateID = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            ProductID = Globals.GetIntFromQueryString("ProductID");

            webapp4U.BOL.Products obj = webapp4U.BOL.Products.GetProductsByID(ProductID);
            CateID = obj.CateID;
            lblProductCode.Text = obj.ProductCode.Trim();
            lblProductName.Text = obj.Title;
            lblNhaSX.Text = Companies.GetCompanyName(obj.CompanyID);
            lblChatlieu.Text = Chatlieu.GetChatlieuNamByChatlieuID(obj.ChatlieuID);
            lblNam.Text = obj.Nam;
            lblTrongluong.Text = obj.Trongluong;

            if (obj.UnitPrice <= 0)
            {
                lblPrice.Text = ResourceManager.GetString("Product_Price_Lienhe");
                //PanelAddToCart.Visible = false;
            }
            else
            {
                lblPrice.Text = Formatter.FormatMoney(obj.UnitPrice) + " VNĐ";
            }
            ImageObject1.ImageUrl = "~/Products/Image.ashx?Size=M2&ImageUrl="+ obj.ImageUrl;
            lblDesc.Text = obj.Description;
            ImageName = obj.ImageUrl;

            this.Load_Title();
        }


        private void Load_Title()
        {
            Category cate = Category.GetCategoryByID(CateID);
            Title.TitleName = Category.GetCateNameByCateID(cate.ParentCateID) + " <img src=" + Globals.ImagesUrl() + "li1.gif align='absmiddle'> " + Category.GetCateNameByCateID(CateID);
        }


        protected void btAddToCart_Click(object sender, EventArgs e)
        {
            if (Globals.CurrentUserName == "")
                Response.Redirect(Globals.ApplicationPath + "Logon.aspx?ReturnUrl=" + Globals.CurrentUrl);
            else
            { 
                Products obj = Products.GetProductsByID(ProductID);
                ShoppingCart shop = new ShoppingCart();
                shop.UserID = Globals.CurrentUserID;
                shop.AddedDate = DateTime.Now;
                shop.ProductID = obj.ProductID;
                shop.Title = obj.Title;
                shop.UnitPrice = obj.UnitPrice;
                shop.Quantity = 1;
                shop.Subtotal = obj.UnitPrice;
                
                ShoppingCart.Insert(shop);
            }

            Response.Redirect(Globals.ApplicationPath + "Products/ShoppingCart.aspx?CateID="+ CateID);
        }
}
}